﻿using System;
using System.Data.SqlClient;
using System.Windows.Forms;
using DataCenter.Interface;
using DataCenter.Interface.Model;

namespace DataCenter.Component.Source
{
    [DataSource("SqlSource", "1.0.0.1", "SQL数据库数据源")]
    public class SqlSourceEntity : ComponentEntity
    {
        public SqlSourceEntity()
        {
            base.ComponentType = ComponentTypes.Source;
        }

        private string dataSource;
        private string initialCatalog;
        private string userId;
        private string password;
        private bool integratedSecurity;

        [Parameter]
        public string DataSource
        {
            get { return dataSource; }
            set { dataSource = value; }
        }

        [Parameter]
        public string InitialCatalog
        {
            get { return initialCatalog; }
            set { initialCatalog = value; }
        }

        [Parameter]
        public string UserId
        {
            get { return userId; }
            set { userId = value; }
        }

        [Parameter]
        public string Password
        {
            get { return password; }
            set { password = value; }
        }

        [Parameter]
        public bool IntegratedSecurity
        {
            get { return integratedSecurity; }
            set { integratedSecurity = value; }
        }

        public override bool Create(SqlCommand command)
        {
            SqlSourceEditForm editForm = new SqlSourceEditForm(this, false);
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                base.ComponentConfig = ToXml().OuterXml;
                Insert(command);
                return true;
            }
            return false;
        }

        public override bool Modify(SqlCommand command)
        {
            SqlSourceEditForm editForm = new SqlSourceEditForm(this, true);
            if (editForm.ShowDialog() == DialogResult.OK)
            {
                base.ComponentConfig = ToXml().OuterXml;
                Update(command);
                return true;
            }
            return false;
        }

        public override TComponent CreateComponent<TComponent>(SqlCommand command)
        {
            ISource component = new SqlSource(this);
            return (TComponent)component;            
        }
    }
}
